package mo.offer_special;

public class L016 {

    public int lengthOfLongestSubstring(String s) {
        if (s.length() <= 1) {
            return s.length();
        }
        int max = 0;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            int index = builder.indexOf(String.valueOf(s.charAt(i)));
            if (index == -1) {
                builder.append(s.charAt(i));
                if (i == s.length()-1) {
                    max = Math.max(max, builder.length());
                }
            } else {
                max = Math.max(max, builder.length());
                if (index < builder.length() - 1) {
                    String right = builder.substring(index+1);
                    builder.setLength(0);
                    builder.append(right);
                } else {
                    builder.setLength(0);
                }
                builder.append(s.charAt(i));
            }
        }
        return max;
    }

    public static void main(String[] args) {
        L016 test = new L016();
        System.out.println(test.lengthOfLongestSubstring("dvdf"));
    }
}
